/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xtd.model.settlement;

import com.xtd.model.employment.Employee;
import com.xtd.model.retail.ShootingOrder;
import com.xtd.util.FormTypeEnum;
import com.xtd.util.PostingTypeEnum;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.Version;
import org.hibernate.annotations.IndexColumn;

/**
 *
 * @author Lu Yuanbin
 */
@Entity
@Table(name = "form")
@NamedQueries({
    @NamedQuery(name = "Form.findAll", query = "SELECT f FROM Form f")})
@Inheritance(strategy = InheritanceType.JOINED)
@PrimaryKeyJoinColumn
public class Form implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @Column(name = "number")
    private String number;
    @Column(name = "createdTime")
    @Temporal(TemporalType.TIMESTAMP)
    private Date createdTime;
    @Column(name = "last_modified_time")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastModifiedTime;
    @Column(name = "type")
    @Enumerated(EnumType.STRING)
    private FormTypeEnum type;
    @Column(name = "cancelled")
    private boolean cancelled;
    @Column(name = "settledTime")
    @Temporal(TemporalType.TIMESTAMP)
    private Date settledTime;
    @Column(name = "version")
    @Version
    private long version;
    @Column(name = "settled")
    private boolean settled;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "form_id")
    @IndexColumn(name = "posting_postion", base = 0)
    private List<Posting> postingCollection;
    @JoinColumn(name = "createdBy", referencedColumnName = "id")
    @ManyToOne
    private Employee createdBy;
    @JoinColumn(name = "last_modified_by", referencedColumnName = "id")
    @ManyToOne
    private Employee lastModifiedBy;
    @Transient
    private BigDecimal totalAmount;
    @Transient
    private BigDecimal totalPayment;
    @Transient
    private BigDecimal totalRefundment;

    public Form() {
    }

    public Form(String number,FormTypeEnum formType) {
        this.type = formType;
        this.number = number;
    }

    public Form(Integer id) {
        this.id = id;
    }

    public Form(Integer id, String number) {
        this.id = id;
        this.number = number;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public Date getCreatedTime() {
        return createdTime;
    }

    public void setCreatedTime(Date createdTime) {
        this.createdTime = createdTime;
    }

    public Date getLastModifiedTime() {
        return lastModifiedTime;
    }

    public void setLastModifiedTime(Date lastModifiedTime) {
        this.lastModifiedTime = lastModifiedTime;
    }

    public FormTypeEnum getType() {
        return type;
    }

    public void setType(FormTypeEnum type) {
        this.type = type;
    }

    public List<Posting> getPostingCollection() {
        return postingCollection;
    }

    public void setPostingCollection(List<Posting> postingCollection) {
        this.postingCollection = postingCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Form)) {
            return false;
        }
        Form other = (Form) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.xtd.model.Form[id=" + id + "]";
    }

    /**
     * @return the totalAmount
     */
    public BigDecimal getTotalAmount() {
        return totalAmount;
    }

    /**
     * @param totalAmount the totalAmount to set
     */
    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }

    /**
     * @return the totalPayment
     */
    public BigDecimal getTotalPayment() {
        totalPayment = BigDecimal.ZERO;
        if (postingCollection != null) {
            for (Posting posting : postingCollection) {
                if (PostingTypeEnum.收入.equals(posting.getType())) {
                    totalPayment = totalPayment.add(posting.getAmount());
                }
            }
        }
        return totalPayment;
    }

    /**
     * @param totalPayment the totalPayment to set
     */
    public void setTotalPayment(BigDecimal totalPayment) {
        this.totalPayment = totalPayment;
    }

    /**
     * @return the totalRefundment
     */
    public BigDecimal getTotalExpense() {
        totalRefundment = BigDecimal.ZERO;
        if (postingCollection != null) {
            for (Posting posting : postingCollection) {
                if (PostingTypeEnum.支出.toString().equals(posting.getType())) {
                    totalRefundment = totalRefundment.add(posting.getAmount());
                }
            }
        }
        return totalRefundment;
    }

    /**
     * @param totalRefundment the totalRefundment to set
     */
    public void setTotalRefundment(BigDecimal totalRefundment) {
        this.totalRefundment = totalRefundment;
    }

    /**
     * @return the cancelled
     */
    public boolean isCancelled() {
        return cancelled;
    }

    /**
     * @param cancelled the cancelled to set
     */
    public void setCancelled(boolean cancelled) {
        this.cancelled = cancelled;
    }

    /**
     * @return the version
     */
    public long getVersion() {
        return version;
    }

    /**
     * @param version the version to set
     */
    public void setVersion(long version) {
        this.version = version;
    }

    /**
     * @return the createdBy
     */
    public Employee getCreatedBy() {
        return createdBy;
    }

    /**
     * @param createdBy the createdBy to set
     */
    public void setCreatedBy(Employee createdBy) {
        this.createdBy = createdBy;
    }

    /**
     * @return the lastModifiedBy
     */
    public Employee getLastModifiedBy() {
        return lastModifiedBy;
    }

    /**
     * @param lastModifiedBy the lastModifiedBy to set
     */
    public void setLastModifiedBy(Employee lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }

    /**
     * @return the settled
     */
    public boolean isSettled() {
        return settled;
    }

    public String getFormInfo() {
        StringBuilder sb = new StringBuilder();
        if (type.equals(FormTypeEnum.摄影)) {
            ShootingOrder so = (ShootingOrder) this;
            sb.append("客户名:").append(so.getMname()).append(",").append(so.getFname()).append("; 应收款:").append(so.getTotalAmount()).append("; 已收款:").append(so.getTotalPayment()).append("; 已退库").append(so.getTotalExpense());
        }
        return sb.toString();
    }

    /**
     * @return the settledTime
     */
    public Date getSettledTime() {
        return settledTime;
    }

    /**
     * @param settledTime the settledTime to set
     */
    public void setSettledTime(Date settledTime) {
        this.settledTime = settledTime;
    }

    /**
     * @param settled the settled to set
     */
    public void setSettled(boolean settled) {
        this.settled = settled;
    }
}
